import { blogPlugin } from "@vuepress/plugin-blog";
import { defaultTheme } from "@vuepress/theme-default";
import { defineUserConfig } from "vuepress";
import { viteBundler } from "@vuepress/bundler-vite";
import { searchPlugin } from "@vuepress/plugin-search";

const navbar = [
  {
    text: "首页",
    link: "/",
  },
  {
    text: "数据可视化",
    children: [
      {
        text: "地理信息",
        children: [
          {
            text: "3D地图库",
            link: "/posts/DataV/map/3dmap.md",
            activeMatch: "^/posts/DataV/map/3dmap",
          },
          {
            text: "在线地图",
            link: "/posts/DataV/map/onlinemap.md",
            activeMatch: "^/posts/DataV/map/onlinemap",
          },
          {
            text: "Web地图库",
            link: "/posts/DataV/map/webmap.md",
            activeMatch: "^/posts/DataV/map/webmap",
          },
          {
            text: "ArcGIS",
            link: "/posts/DataV/map/arcgis.md",
            activeMatch: "^/posts/DataV/map/arcgis",
          },
          {
            text: "超图",
            link: "/posts/DataV/map/supermap.md",
            activeMatch: "^/posts/DataV/map/supermap",
          },
          {
            text: "地图工具 | 插件",
            link: "/posts/DataV/map/else.md",
            activeMatch: "^/posts/DataV/map/else",
          },
        ],
      },
      {
        text: "图表 | 图形分析",
        children: [
          {
            text: "图表",
            link: "/posts/DataV/graph/chart.md",
            activeMatch: "^/posts/DataV/graph/chart",
          },
          {
            text: "流程图",
            link: "/posts/DataV/graph/flow.md",
            activeMatch: "^/posts/DataV/graph/flow",
          },
        ],
      },
      {
        text: "3D图形 | 模型",
        children: [
          {
            text: "开源WebGL库",
            link: "/posts/DataV/3d/webgl.md",
            activeMatch: "^/posts/DataV/3d/webgl",
          },
        ],
      },
      {
        text: "可视化方案",
        children: [
          {
            text: "AntV",
            link: "/posts/DataV/visualization/antv.md",
            activeMatch: "^/posts/DataV/visualization/antv",
          },
          {
            text: "DataV",
            link: "/posts/DataV/visualization/datav.md",
            activeMatch: "^/posts/DataV/visualization/datav",
          },
        ],
      },
    ],
  },
  {
    text: "文章",
    link: "/article/",
  },
  {
    text: "分类",
    link: "/category/",
  },
  {
    text: "标签",
    link: "/tag/",
  },
  {
    text: "时间线",
    link: "/timeline/",
  },
];

export default defineUserConfig({
  base: "/my-space/",
  lang: "en-US",

  title: "我的空间",
  description: "欢迎来到我的空间",

  theme: defaultTheme({
    logo: "https://vuejs.press/images/hero.png",
    navbar: navbar,
  }),

  plugins: [
    searchPlugin({
      // 插件选项
      locales: {
        "/": {
          placeholder: "搜索",
        },
        "/zh/": {
          placeholder: "搜索",
        },
      },
      maxSuggestions: 10,
    }),
    blogPlugin({
      // Only files under posts are articles
      filter: ({ filePathRelative }) =>
        filePathRelative ? filePathRelative.startsWith("posts/") : false,

      // Getting article info
      getInfo: ({ frontmatter, title, data }) => ({
        title,
        author: frontmatter.author || "",
        date: frontmatter.date || null,
        category: frontmatter.category || [],
        tag: frontmatter.tag || [],
        excerpt:
          // Support manually set excerpt through frontmatter
          typeof frontmatter.excerpt === "string"
            ? frontmatter.excerpt
            : data?.excerpt || "",
      }),

      // Generate excerpt for all pages excerpt those users choose to disable
      excerptFilter: ({ frontmatter }) =>
        !frontmatter.home &&
        frontmatter.excerpt !== false &&
        typeof frontmatter.excerpt !== "string",

      category: [
        {
          key: "category",
          getter: (page) => page.frontmatter.category || [],
          layout: "Category",
          itemLayout: "Category",
          frontmatter: () => ({
            title: "Categories",
            sidebar: false,
          }),
          itemFrontmatter: (name) => ({
            title: `Category ${name}`,
            sidebar: false,
          }),
        },
        {
          key: "tag",
          getter: (page) => page.frontmatter.tag || [],
          layout: "Tag",
          itemLayout: "Tag",
          frontmatter: () => ({
            title: "Tags",
            sidebar: false,
          }),
          itemFrontmatter: (name) => ({
            title: `Tag ${name}`,
            sidebar: false,
          }),
        },
      ],

      type: [
        {
          key: "article",
          // Remove archive articles
          filter: (page) => !page.frontmatter.archive,
          layout: "Article",
          frontmatter: () => ({
            title: "Articles",
            sidebar: false,
          }),
          // Sort pages with time and sticky
          sorter: (pageA, pageB) => {
            if (pageA.frontmatter.sticky && pageB.frontmatter.sticky)
              return pageB.frontmatter.sticky - pageA.frontmatter.sticky;

            if (pageA.frontmatter.sticky && !pageB.frontmatter.sticky)
              return -1;

            if (!pageA.frontmatter.sticky && pageB.frontmatter.sticky) return 1;

            if (!pageB.frontmatter.date) return 1;
            if (!pageA.frontmatter.date) return -1;

            return (
              new Date(pageB.frontmatter.date).getTime() -
              new Date(pageA.frontmatter.date).getTime()
            );
          },
        },
        {
          key: "timeline",
          // Only article with date should be added to timeline
          filter: (page) => page.frontmatter.date instanceof Date,
          // Sort pages with time
          sorter: (pageA, pageB) =>
            new Date(pageB.frontmatter.date).getTime() -
            new Date(pageA.frontmatter.date).getTime(),
          layout: "Timeline",
          frontmatter: () => ({
            title: "Timeline",
            sidebar: false,
          }),
        },
      ],
      hotReload: true,
    }),
  ],

  bundler: viteBundler(),
});
